package com.project.common.core.template;

import com.project.common.constant.HttpStatus;
import com.project.common.utils.StringUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import com.project.common.constant.ResultCode;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;


/**
 * 系统响应规范
 *
 * @Author: 汪焰
 * @Date: 2020/6/8 11:09
 **/
@Slf4j
@Data
@ApiModel(value = "系统响应规范")
public class Result<T> {

    @ApiModelProperty(value = "状态码:200成功")
    private int code = ResultCode.SUCCESS.getCode();
    @ApiModelProperty(value = "响应描述:success成功")
    private String msg = ResultCode.SUCCESS.getMsg();
    @ApiModelProperty(value = "响应详细-描述:success成功")
    private String msgDetail = ResultCode.SUCCESS.getMsg();
    @ApiModelProperty(value = "响应数据")
    private T data;

    public static Result success() {
        return new Result(ResultCode.SUCCESS);
    }

    public static <T> Result<T> success(T data) {
        return new Result(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), data);
    }

    public static Result error(ResultCode resultCode) {
        return new Result(resultCode);
    }

    public static Result error(int code, String msg) {
        return new Result(code, msg, null);
    }

    public static Result error(String msg) {
        return new Result(ResultCode.ERROR.getCode(), msg, null);
    }
    public static Result error(int error, String msg, Throwable e) {
        Result result = new Result(error, null, msg);

        result.setMsgDetail(StringUtils.getErrorMsg(e, 3000));
        return result;
    }

    /**
     * 数据不能重复
     *
     * @Author: 汪焰
     * @Date: 2020/6/13 16:31
     * version 1.0.0
     **/
    public static Result paramsError(String msg) {
        return new Result(ResultCode.DATA_ERROR.getCode(), msg, null);
    }


    private Result(ResultCode resultCode) {
        this.code = resultCode.getCode();
        this.msg = resultCode.getMsg();
    }

    private Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static Map newMapSuccess() {
        Map<String, Object> result = new HashMap<>();
        result.put("code", ResultCode.SUCCESS.getCode());
        result.put("msg", ResultCode.SUCCESS.getMsg());
        return result;
    }
}
